<template>
  <div class="container">
    <div class="header" style="margin-bottom: 20px">
      <a-button type="primary" icon="plus" @click="add">新增商品</a-button>
      <div class="input">
        <a-input placeholder="产品名称" v-model="productName" />
      </div>

      <a-button type="primary" icon="search" @click="serach">搜索</a-button>
    </div>
    <a-table
      :columns="columns"
      :data-source="rows"
      :rowKey="(record) => record.id"
      :scroll="{ x: 1000 }"
    >
      <a slot="action">删除</a>
    </a-table>

    <div class="popup" v-show="commodity_popup">
      <a-form-model
        :model="form"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item label="产品名称">
          <a-input v-model="form.productName" />
        </a-form-model-item>
        <a-form-model-item label="周期">
          <a-select v-model="form.planningCycle" placeholder="选择周期">
            <a-select-option value=" 30天/期"> 30天/期 </a-select-option>
            <a-select-option value="20天/期"> 20天/期 </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="消费额度">
          <a-input v-model="form.consumptionLimit" placeholder="额度" />
        </a-form-model-item>
        <a-form-model-item label="产品型号">
          <a-select v-model="form.productNumber" placeholder="型号">
            <a-select-option value=" 300型"> 300型 </a-select-option>
            <a-select-option value="500型"> 500型 </a-select-option>
            <a-select-option value=" 600型"> 600型 </a-select-option>
            <a-select-option value="1000型"> 1000型 </a-select-option>
          </a-select>
        </a-form-model-item>

        <a-form-model-item label="省钱率">
          <a-select v-model="form.moneyIssueRate" placeholder="省钱率为百分比">
            <a-select-option value=" 300型"> 0 </a-select-option>
            <a-select-option value="500型"> 0.1</a-select-option>
            <a-select-option value=" 600型"> 0.2 </a-select-option>
            <a-select-option value="0.3"> 0.3 </a-select-option>
            <a-select-option value=" 0.4"> 0.4 </a-select-option>
            <a-select-option value="0.5"> 0.5 </a-select-option>
            <a-select-option value=" 0.6"> 0.6 </a-select-option>
            <a-select-option value="0.7"> 0.7 </a-select-option>
            <a-select-option value=" 0.8"> 0.8 </a-select-option>
            <a-select-option value="0.9"> 0.9 </a-select-option>
            <a-select-option value=" 1"> 1 </a-select-option>
          </a-select>
        </a-form-model-item>

        <a-form-model-item label="发钱率一期">
          <a-input v-model="form.moneySavingRate" />
        </a-form-model-item>
        <a-form-model-item label="发钱率二期">
          <a-input v-model="form.moneySavingRate" />
        </a-form-model-item>

        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button @click="cancel"> 取消 </a-button>
          <a-button type="primary" @click="onSubmit" style="margin-left: 10px">
            确定
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
// const { mapActions } = createNamespacedHelpers("commodity");
export default {
  data() {
    return {
      columns,
      productName: "",
      commodity_popup:false,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        productName: "",
        moneyIssueRate: "",
        productNumber: "",
        planningCycle: "",
        consumptionLimit: "",
        moneySavingRate: "",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // ...mapActions({
    //   get: "commodity/get",
    // }),
    getList() {
      this.$store.dispatch("commodity/get");
    },
    serach() {
      console.log(this.productName);
      this.$store.commit("commodity/serach", this.productName);
      this.getList();
    },
    add(){
      this.commodity_popup =!this.commodity_popup
    },
    cancel(){
       this.commodity_popup =!this.commodity_popup
    },
    onSubmit() {
      console.log("submit!", this.form);
    },
  },
  computed: {
    ...mapState({
      rows: (state) => state.commodity.rows,
      total: (state) => state.commodity.total,
    }),
  },
};

const columns = [
  { title: "ID", dataIndex: "id", key: "id", fixed: "left" },
  { title: "产品名称", dataIndex: "productName", key: "productName" },
  { title: "省钱率", dataIndex: "moneySavingRate", key: "moneySavingRate" },
  { title: "产品型号", dataIndex: "productNumber", key: "productNumber" },
  {
    title: "操作",
    key: "operation",
    fixed: "right",
    scopedSlots: { customRender: "action" },
  },
];
const data = [];
for (let i = 0; i < 100; i++) {
  data.push({
    key: i,
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
  });
}

// console.log(this.rows);
</script>

<style>
.container {
  width: 100%;
  height: 100%;
}
.header {
  display: flex;
}
.input {
  width: 200px;
  display: flex;
  margin: 0 10px 0 30px;
}
.popup {
  border-radius: 15px;
  background-clip: padding-box;
  width: 550px;
  height: 520px;
  padding: 15px 35px 15px 35px;
  background: #fefefe;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  z-index: 1;
}
</style>
